package yunwo.cn.yzstation.model;

import android.content.SharedPreferences;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.request.RequestCall;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import yunwo.cn.yzstation.api.HttpManager;
import yunwo.cn.yzstation.api.okapi.DictionCallBack;
import yunwo.cn.yzstation.api.okapi.GLCallBack;
import yunwo.cn.yzstation.api.ImainModel;
import yunwo.cn.yzstation.base.Yzapp;
import yunwo.cn.yzstation.bean.DictionariesBean;
import yunwo.cn.yzstation.bean.UserInfoBean;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.view.activity.MainActivity;

/**
 * Created by lao on 2017/9/7.
 */

public class MainActivityModel {
    MainActivity mMainActivity;
    ImainModel mImainModel;

    public MainActivityModel(MainActivity mainActivity, ImainModel imainModel) {
        this.mMainActivity = mainActivity;
        this.mImainModel = imainModel;

    }


    //获取用户信息
    public void getUserInfo() {

        Map<String, String> map = new HashMap<>();
        map.put("params", Yzapp.getInstance().getmobile());
        HttpManager.hpptPostString(MyConstants.userinfo,map).execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    if (!result.equals("")) {
                        UserInfoBean.DataBean dataBean = new Gson().fromJson(result, UserInfoBean.DataBean.class);
                        mImainModel.requestUserInfoSuccess(dataBean);
                    }
                }else {
                    Toast.makeText(mMainActivity,msg, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(mMainActivity,"请求失败", Toast.LENGTH_SHORT).show();
            }
        });
//        HttpManager.hpptGet(MyConstants.userinfo, map).execute();
  /*      HttpManager.hpptPostString3(MyConstants.userinfo,map).execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    if (!result.equals("")) {
                        UserInfoBean.DataBean dataBean = new Gson().fromJson(result, UserInfoBean.DataBean.class);
                        mImainModel.requestUserInfoSuccess(dataBean);
                    }
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {

            }
        });*/

    }
    /*new UserinfoCallBack(UserInfoBean.class,mMainActivity,false){
            @Override
            public void myError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(UserInfoBean response, int id) {

                if (response.getCode() == 200) {

                    mImainModel.requestUserInfoSuccess(response);
                }
                else {
                    Toast.makeText(mMainActivity, response.getCode() + response.getMsg(), Toast.LENGTH_SHORT).show();
                }
            }
        }*/
                //获取字典
    public void getDictionariesData() {
        final SharedPreferences sp = mMainActivity.getSharedPreferences("SP_DICTION",mMainActivity.MODE_PRIVATE);


        RequestCall build = OkHttpUtils.post().url(MyConstants.DictionariesData)
                .addHeader("Authorization", Yzapp.getInstance().getAuthorization())
                .build();
        build.execute(new DictionCallBack(DictionariesBean.class, mMainActivity, false){
            @Override
            public void onResponse(DictionariesBean response, int id) {

                Gson gson = new Gson();
                String json = gson.toJson(response);
                SharedPreferences.Editor edit = sp.edit();
                edit.putString("zidian", json);
                edit.commit();


            }

            @Override
            public void myError(Call call, Exception e, int id) {

            }
        });
    }

}
